package com.up.management.service.impl;

import com.up.management.domain.entity.InheritorApplication;
import com.up.management.domain.entity.Inheritor;
import com.up.management.mapper.InheritorApplicationMapper;
import com.up.management.mapper.InheritorMapper;
import com.up.management.service.IInheritorApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 传承人申请管理Service业务层处理
 * 
 * @author cloud
 * @date 2025-09-22
 */
@Service
public class InheritorApplicationServiceImpl implements IInheritorApplicationService 
{
    @Autowired
    private InheritorApplicationMapper inheritorApplicationMapper;

    @Autowired
    private InheritorMapper inheritorMapper;

    /**
     * 查询传承人申请管理
     * 
     * @param id 传承人申请管理主键
     * @return 传承人申请管理
     */
    @Override
    public InheritorApplication selectInheritorApplicationById(Long id)
    {
        return inheritorApplicationMapper.selectInheritorApplicationById(id);
    }

    /**
     * 查询传承人申请管理列表
     * 
     * @param inheritorApplication 传承人申请管理
     * @return 传承人申请管理
     */
    @Override
    public List<InheritorApplication> selectInheritorApplicationList(InheritorApplication inheritorApplication)
    {
        return inheritorApplicationMapper.selectInheritorApplicationList(inheritorApplication);
    }

    /**
     * 新增传承人申请管理
     * 
     * @param inheritorApplication 传承人申请管理
     * @return 结果
     */
    @Override
    public int insertInheritorApplication(InheritorApplication inheritorApplication)
    {
        return inheritorApplicationMapper.insertInheritorApplication(inheritorApplication);
    }

    /**
     * 修改传承人申请管理
     * 
     * @param inheritorApplication 传承人申请管理
     * @return 结果
     */
    @Override
    public int updateInheritorApplication(InheritorApplication inheritorApplication)
    {
        return inheritorApplicationMapper.updateInheritorApplication(inheritorApplication);
    }

    /**
     * 审核传承人申请
     * 
     * @param inheritorApplication 传承人申请管理
     * @return 结果
     */
    @Override
    @Transactional
    public int auditInheritorApplication(InheritorApplication inheritorApplication)
    {
        // 1. 更新申请状态
        int result = inheritorApplicationMapper.updateInheritorApplication(inheritorApplication);
        
        // 2. 如果审核通过（状态为2），则添加到传承人表
        if (result > 0 && Long.valueOf(2).equals(inheritorApplication.getStatus())) {
            // 查询完整的申请信息
            InheritorApplication fullApplication = inheritorApplicationMapper.selectInheritorApplicationById(inheritorApplication.getId());
            
            // 创建传承人对象
            Inheritor inheritor = new Inheritor();
            inheritor.setUserId(fullApplication.getUserId());
            inheritor.setItemId(fullApplication.getItemId());
            inheritor.setRealName(fullApplication.getRealName());
            inheritor.setName(fullApplication.getName());
            inheritor.setBiography(fullApplication.getExperience()); // experience -> biography
            inheritor.setContact(fullApplication.getContact());
            inheritor.setProofMaterials(fullApplication.getProofMaterials());
            inheritor.setRemark(inheritorApplication.getRemark());
            inheritor.setCreatedAt(new java.util.Date());
            inheritor.setUpdatedAt(new java.util.Date());
            
            // 插入传承人记录
            inheritorMapper.insertInheritor(inheritor);
        }
        
        return result;
    }

    /**
     * 批量删除传承人申请管理
     * 
     * @param ids 需要删除的传承人申请管理主键
     * @return 结果
     */
    @Override
    public int deleteInheritorApplicationByIds(Long[] ids)
    {
        return inheritorApplicationMapper.deleteInheritorApplicationByIds(ids);
    }

    /**
     * 删除传承人申请管理信息
     * 
     * @param id 传承人申请管理主键
     * @return 结果
     */
    @Override
    public int deleteInheritorApplicationById(Long id)
    {
        return inheritorApplicationMapper.deleteInheritorApplicationById(id);
    }
}
